<?php
/**
 * CakePOWER
 * Language Management Configuration
 * 
 * @author:			Marco Pegoraro
 * @mail:			marco(dot)pegoraro(at)gmail(dot)com
 * 
 * 25 September 2009
 * This file handle url-lang request.
 */

/**
 * Get the requested url from the global param and clean it to be used.
 */
$__url__ = $_GET['url'];
if ( substr($__url__,0,1) == '/' ) $__url__ = substr($__url__,1,255);
if ( substr($__url__,strlen($__url__)-1,1) != '/' ) $__url__ .= '/';

/**
 * Dictionary Mode
 */
if ( DAC::read('power.lang.flag') == '' && DAC::read('power.lang.flag') !== false ) {
	
	// It try to extract first param from the requested url.                                      #
	$__lang__ = substr( $__url__, 0, strpos( $__url__, '/' ) );
	if ( empty($__lang__) ) $__lang__ = $__url__;
	if ( in_array($__lang__,DAC::read('power.lang.dictionary')) !== false ) {
		DAC::write('power.lang.flag',$__lang__);
		$_GET['url'] = '/'.substr( $__url__, strlen($__lang__)+1, 255 );
	}
	
}

/**
 * Lang Length Mode.
 */
if ( DAC::read('power.lang.flag') == '' && DAC::read('power.lang.flag') !== false && DAC::read('power.lang.length') > 0 ) {
	
	$__lang__ = substr( $__url__, 0, strpos( $__url__, '/' ) );
	
	// It manages language exceptions.                                                            #
	if ( in_array($__lang__,DAC::read('power.lang.exceptions')) ) $__lang__ = false;
	
	if ( $__lang__ && ( strlen( $__lang__ ) == DAC::read('power.lang.length') || strLen($__url__) == DAC::read('power.lang.length') ) ) {
		DAC::write( 'power.lang.flag', substr( $__url__, 0, DAC::read('power.lang.length') ) );
		$_GET['url'] = '/'.substr( $__url__, DAC::read('power.lang.length')+1, 255 );
	}
	
}

/**
 * Default Lang Flag.
 */
if ( !DAC::read('power.lang.flag') && DAC::read('power.lang.flag') !== false )
	DAC::write('power.lang.flag', DAC::read('power.lang.default') );

/**
 * Clean local variables.
 */
unset($__url__);
unset($__lang__);

/**
 * Set a DAC value.
 */
DAC::write('sys.lang', DAC::read('power.lang.flag') );
?>